package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.AdminAccountMapper;
import com.example.online_order.mapper.DeskMapper;
import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.Desk;
import com.example.online_order.service.DeskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DeskServiceImpl implements DeskService {
    @Autowired
    DeskMapper mapper;
    @Override
    public List<Desk> getAllDesks() {
        return mapper.getAllDesks();
    }

   @Override
    public Desk getDeskById(Integer id) {
        return mapper.getDeskById(id);
    }

    @Transactional
    @Override
    public Integer addDesk(Desk desk) {
       Desk deskDb = mapper.getDeskById(desk.getId());
        if (deskDb != null){
            throw new BussinessException(ExceptionType.bus_id_exists,"桌子已存在");
        }else {
            Integer res = mapper.addDesk(desk);
            if (res != 1){
                throw new BussinessException(ExceptionType.bus_insert_fail,"添加桌子失败");
            }else {
                return res;
            }
        }

    }
    @Transactional
    @Override
    public Integer deleteDesk(Integer id) {
        Desk deskDb = mapper.getDeskById(id);
        if (deskDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"桌子不存在");
        }else {
            Integer res = mapper.deleteDesk(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_delete_fail,"删除桌子失败");
            }else {
                return res;
            }
        }

    }
}
